package com.web.exiaodao.remote.service.impl;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.entity.ServiceDomesticNursing;
import com.web.exiaodao.business.repository.ServiceDomesticNursingRepository;
import com.web.exiaodao.remote.service.ApiServiceDomesticNursingService;
import com.web.exiaodao.remote.vo.ServiceDomesticNursingVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zhangl
 * @times: 2016-11-03 18:26:50
 * 类的说明：前端APIDomesticNursing接口实现类
 **/
@Service("ApiDomesticNursingService")
@Transactional(readOnly = false)
public class ApiServiceDomesticNursingServiceImpl extends BaseService<ServiceDomesticNursing> implements ApiServiceDomesticNursingService {

	private static final Logger LOGGER = Logger.getLogger(ApiServiceDomesticNursingServiceImpl.class);

	@Autowired
	ServiceDomesticNursingRepository serviceDomesticNursingRepository;

	@Override
	public XaResult<ServiceDomesticNursingVo> findOne(Long tId) throws BusinessException {
		ServiceDomesticNursing obj = serviceDomesticNursingRepository.findByIdAndStatusNot(tId,XaConstant.Status.delete);
		//保存点击量
        obj.setClickCont(1 + NumberUtils.toLong(obj.getClickCont()+""));
        serviceDomesticNursingRepository.save(obj);
		
		XaResult<ServiceDomesticNursingVo> xr = new XaResult<ServiceDomesticNursingVo>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj),ServiceDomesticNursingVo.class));
			xr.getObject().setId(obj.getId());
			LOGGER.info("查询单个实体----->ServiceDomesticNursing");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
			throw new BusinessException(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	@Override
	public XaResult<ServiceDomesticNursingVo> findLastOne() throws BusinessException {
		XaResult<ServiceDomesticNursingVo> xr = new XaResult<>();
		Map<String, SearchFilter> filters = new HashMap<>();
		filters.put("status", new SearchFilter("status", Operator.NE, XaConstant.Status.delete));
		List<ServiceDomesticNursing> entitys = serviceDomesticNursingRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ServiceDomesticNursing.class), new Sort(
				new Sort.Order[]{
						new Sort.Order(Sort.Direction.DESC, "createTime"),
						new Sort.Order(Sort.Direction.DESC, "id")
				}));
		if (entitys == null || entitys.size() == 0) {
			xr.error("暂无信息");
		} else {
			xr.setObject(JSON.parseObject(JSON.toJSONString(entitys.get(0)), ServiceDomesticNursingVo.class));
			xr.getObject().setId(entitys.get(0).getId());
			LOGGER.info("查询单个实体----->ServiceDomesticNursing");
		}
		return xr;
	}

	@Override
	public XaResult<List<ServiceDomesticNursingVo>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<ServiceDomesticNursing> page = serviceDomesticNursingRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ServiceDomesticNursing.class), pageable);
		XaResult<List<ServiceDomesticNursingVo>> xr = new XaResult<List<ServiceDomesticNursingVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ServiceDomesticNursingVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<List<ServiceDomesticNursingVo>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<ServiceDomesticNursing> page = serviceDomesticNursingRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ServiceDomesticNursing.class), pageable);
		XaResult<List<ServiceDomesticNursingVo>> xr = new XaResult<List<ServiceDomesticNursingVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ServiceDomesticNursingVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<ServiceDomesticNursingVo> multiOperate(String modelIds,
														   Integer status) throws BusinessException {
		XaResult<ServiceDomesticNursingVo> xr = new XaResult<ServiceDomesticNursingVo>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				ServiceDomesticNursing obj = serviceDomesticNursingRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = serviceDomesticNursingRepository.save(obj);
					xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ServiceDomesticNursingVo.class));
					xr.getObject().setId(obj.getId());
				}
			}
		}
		LOGGER.info("更改数据状态----->ServiceDomesticNursing");
		return xr;
	}

	@Override
	public XaResult<ServiceDomesticNursingVo> createModel(ServiceDomesticNursing model)
			throws BusinessException {
		XaResult<ServiceDomesticNursingVo> xr = new XaResult<ServiceDomesticNursingVo>();
		ServiceDomesticNursing obj = serviceDomesticNursingRepository.save(model);
		xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ServiceDomesticNursingVo.class));
		xr.getObject().setId(obj.getId());
		LOGGER.info("新增数据成功----->ServiceDomesticNursing");
		return xr;
	}

	@Override
	public XaResult<ServiceDomesticNursingVo> updateModel(ServiceDomesticNursing model)
			throws BusinessException {
		ServiceDomesticNursing obj = serviceDomesticNursingRepository.findOne(model.getId());
		XaResult<ServiceDomesticNursingVo> xr = new XaResult<ServiceDomesticNursingVo>();
		if (XaUtil.isNotEmpty(obj)) {
			obj.setNurse_Name(XaUtil.isNotEmpty(model.getNurse_Name()) ? model.getNurse_Name() : obj.getNurse_Name());
			obj.setNurse_RoleID(XaUtil.isNotEmpty(model.getNurse_RoleID()) ? model.getNurse_RoleID() : obj.getNurse_RoleID());
			obj.setNurse_Sex(XaUtil.isNotEmpty(model.getNurse_Sex()) ? model.getNurse_Sex() : obj.getNurse_Sex());
			obj.setNurse_Age(XaUtil.isNotEmpty(model.getNurse_Age()) ? model.getNurse_Age() : obj.getNurse_Age());
			obj.setNurse_LengthServi(XaUtil.isNotEmpty(model.getNurse_LengthServi()) ? model.getNurse_LengthServi() : obj.getNurse_LengthServi());
			obj.setNurse_Native(XaUtil.isNotEmpty(model.getNurse_Native()) ? model.getNurse_Native() : obj.getNurse_Native());
			obj.setNurse_Salary(XaUtil.isNotEmpty(model.getNurse_Salary()) ? model.getNurse_Salary() : obj.getNurse_Salary());
			obj.setNurse_PicUrl(XaUtil.isNotEmpty(model.getNurse_PicUrl()) ? model.getNurse_PicUrl() : obj.getNurse_PicUrl());
			obj.setNurse_Cellphone(XaUtil.isNotEmpty(model.getNurse_Cellphone()) ? model.getNurse_Cellphone() : obj.getNurse_Cellphone());
			obj.setNurse_Education(XaUtil.isNotEmpty(model.getNurse_Education()) ? model.getNurse_Education() : obj.getNurse_Education());
			obj.setNurse_Salary_unit(XaUtil.isNotEmpty(model.getNurse_Salary_unit()) ? model.getNurse_Salary_unit() : obj.getNurse_Salary_unit());
			obj = serviceDomesticNursingRepository.save(obj);
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ServiceDomesticNursingVo.class));
			xr.getObject().setId(obj.getId());
		} else {
			xr.error(XaConstant.Message.object_not_find);
		}
		LOGGER.info("编辑数据成功----->ServiceDomesticNursing");
		return xr;
	}

}
